package com.hengtongxing.hejiayun.homepage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.BaseResponseBean;
import com.hengtongxing.hejiayun.bean.MyHouseListBean;
import com.hengtongxing.hejiayun.dialog.MyHouseDialog;
import com.hengtongxing.hejiayun.homepage.model.HomePageModelImpl;
import com.hengtongxing.hejiayun.homepage.model.IHomePageModel;
import com.hengtongxing.hejiayun.mine.model.MineModelImpl;
import com.hengtongxing.hejiayun.utils.ToastUtil;
import com.hengtongxing.hejiayun.widget.AppTitleBar;
import com.hengtongxing.hejiayun.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 咨询
 */
public class ConsultServiceActivity extends BaseActivity {


    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_house)
    TextView tvHouse;
    @BindView(R.id.ll_select_house)
    LinearLayout llSelectHouse;
    @BindView(R.id.edit_info)
    EditText editInfo;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private IHomePageModel homePageModel;
    private String hose_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consult_service);
        ButterKnife.bind(this);
        initView();
        requestMyHouse();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();
        titleBar.setRightClickListener(() -> {
            showActivity(ConsultListActivity.class);
        });
    }

    public void requestMyHouse() {
        new MineModelImpl().requestMyHouse(new DataCallBack<MyHouseListBean>() {
            @Override
            public void onSuccessful(MyHouseListBean bean) {
                if (bean.getData().size() > 0) {
                    for (MyHouseListBean.DataBean dataBean : bean.getData()) {
                        if (dataBean.getIs_def() == 1) {
                            String address = dataBean.getComm_name() + dataBean.getBuilding() + dataBean.getUnit_name() + dataBean.getFloor() + dataBean.getHose_number();
                            tvHouse.setText(address);
                            hose_id = dataBean.getId() + "";
                            return;
                        }
                    }
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_select_house, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_select_house:
                MyHouseDialog dialog = MyHouseDialog.newInstance();
                dialog.show(getSupportFragmentManager(), "dialog");
                dialog.setOnCallBackListener((name, id) -> {
                    tvHouse.setText(name);
                    hose_id = id;
                });
                break;
            case R.id.tv_commit:
                if (TextUtils.isEmpty(hose_id)) {
                    ToastUtil.showShort("请选择房屋");
                    return;
                }
                String info = editInfo.getText().toString();
                if (TextUtils.isEmpty(info)) {
                    ToastUtil.showShort("请输入问题详情");
                    return;
                }
                requestHousesAdvisoryAdd(info);
                break;
        }
    }

    public void requestHousesAdvisoryAdd(String info) {
        ViewLoading.showProgress(mActivity, "提交中......");
        homePageModel.requestHousesAdvisoryAdd(hose_id, info, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("提交成功");
                showActivity(ConsultListActivity.class);
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
